package org.andao.core.utils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.StringUtils;

/**
 * 提供常用String类型数据工具. <p>
 * 该类继承了apache 的StringUtils，可以自行扩展.<p>
 * @author Marco.hu(hzg139@163.com)
 */
public class ExStringUtils extends StringUtils{
	
	/**定义HTML escape 替换字符*/
	 private final static byte[] val = { 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01,       
         0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,       
         0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F }; 
	
	/**
	 * 随即生产随即数，可以用来生产tocken字符串等.
	 * @param length 生成长度
	 * @return 随即数字符串.
	 */
	public static String getRandomString(int length) {
		StringBuffer bu = new StringBuffer();
		String[] arr = { "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
				"m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
				"H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
		Random random = new Random();
		while (bu.length() < length) {
			String temp = arr[random.nextInt(57)];
			if (bu.indexOf(temp) == -1) {
				bu.append(temp);
			}
		}
		return bu.toString();
	}
	
	/**
	 * 获取某个区间的随机整数.
	 * @param sek 随机种子
	 * @param min 最小整数
	 * @param max 最大整数
	 * @return 随机整数
	 */
	public static int getRandomInt(int sek, int min, int max) {
		Random random = new Random();
		int temp = 0;
		do {
			temp = random.nextInt(sek);
		} while (temp < min || temp > max);
		return temp;
	}
	
	/**
	 * 实现了js 版本的unescape方法.<p>
	 * 用于前台JS对传入的参数做了escape，JAVA后台取到参数后需要unescape。
	 * @param s
	 * @return
	 */
	 public static String unescape(String s) {       
         StringBuffer sbuf = new StringBuffer();       
         int i = 0;       
         int len = s.length();       
         while (i < len) {       
         int ch = s.charAt(i);       
         if ('A' <= ch && ch <= 'Z') {   
         sbuf.append((char) ch);       
         } else if ('a' <= ch && ch <= 'z') {        
         sbuf.append((char) ch);       
         } else if ('0' <= ch && ch <= '9') {    
             sbuf.append((char) ch);       
         } else if (ch == '-' || ch == '_'|| ch == '.' || ch == '!' || ch == '~' || ch == '*'|| ch == '\'' || ch == '(' || ch == ')') {       
         sbuf.append((char) ch);       
         } else if (ch == '%') {   
             int cint = 0;       
             if ('u' != s.charAt(i + 1)) {       
             cint = (cint << 4) | val[s.charAt(i + 1)];       
             cint = (cint << 4) | val[s.charAt(i + 2)];       
             i += 2;       
             } else {       
                 cint = (cint << 4) | val[s.charAt(i + 2)];       
                 cint = (cint << 4) | val[s.charAt(i + 3)];       
                 cint = (cint << 4) | val[s.charAt(i + 4)];       
                 cint = (cint << 4) | val[s.charAt(i + 5)];       
                 i += 5;       
             }       
             sbuf.append((char) cint);   
         } else {       
             sbuf.append((char) ch);       
         }       
         i++;       
         }       
         return sbuf.toString();       
	 }  
	 
	 /**
	  * 转换中文字符
	  * @param str
	  * @return
	  */
	 public static String getChinesStr(String str){
		 try {
			return new String(str.getBytes("gb2312"), "iso-8859-1");
		} catch (UnsupportedEncodingException e) {			
			return str;
		}
	 }
	 
	 /**
	  * 全角转半角的 转换函数  
	  * @param str
	  * @return
	  */
	  public static String full2HalfChange(String str) {  	        
		  char c[] = str.toCharArray();
          for (int i = 0; i < c.length; i++) {
            if (c[i] == '\u3000') {
            	c[i] = ' ';
            } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
            	c[i] = (char) (c[i] - 65248);
            }
          }
          return new String(c);
	   } 
	  
	  //html->text
	 public static String trimHtml2Txt(String html, String[] filterTags){	
			html = html.replaceAll("\\<head>[\\s\\S]*?</head>(?i)", "");//去掉head
			html = html.replaceAll("\\<!--[\\s\\S]*?-->", "");//去掉注释
			html = html.replaceAll("\\<![\\s\\S]*?>", "");
			html = html.replaceAll("\\<style[^>]*>[\\s\\S]*?</style>(?i)", "");//去掉样式
			html = html.replaceAll("\\<script[^>]*>[\\s\\S]*?</script>(?i)", "");//去掉js
			html = html.replaceAll("\\<w:[^>]+>[\\s\\S]*?</w:[^>]+>(?i)", "");//去掉word标签
			html = html.replaceAll("\\<xml>[\\s\\S]*?</xml>(?i)", "");
			html = html.replaceAll("\\<html[^>]*>|<body[^>]*>|</html>|</body>(?i)", "");
			html = html.replaceAll("\\\r\n|\n|\r", " ");//去掉换行
			html = html.replaceAll("\\<br[^>]*>(?i)", "\n");
			List<String> tags = new ArrayList<String>();
			List<String> s_tags = new ArrayList<String>();
			List<String> halfTag = Arrays.asList(new String[]{"img","table","thead","th","tr","td"});//
			if(filterTags != null && filterTags.length > 0){
				for (String tag : filterTags) {
					tags.add("<"+tag+(halfTag.contains(tag)?"":">"));//开始标签
					if(!"img".equals(tag)) tags.add("</"+tag+">");//结束标签
					s_tags.add("#REPLACETAG"+tag+(halfTag.contains(tag)?"":"REPLACETAG#"));//尽量替换为复杂一点的标记,以免与显示文本混合,如：文本中包含#td、#table等
					if(!"img".equals(tag)) s_tags.add("#REPLACETAG/"+tag+"REPLACETAG#");
				}
			}
			html = ExStringUtils.replaceEach(html, tags.toArray(new String[tags.size()]), s_tags.toArray(new String[s_tags.size()]));				
			html = html.replaceAll("\\</p>(?i)", "\n");
			html = html.replaceAll("\\<[^>]+>", "");
			html = ExStringUtils.replaceEach(html,s_tags.toArray(new String[s_tags.size()]),tags.toArray(new String[tags.size()]));
			html = html.replaceAll("\\&nbsp;", " ");
			return html.trim();
		}
  
}